/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package common;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 *
 * @author Donghui
 */
public class OffsetDate {
    private static OffsetDate local;
    private Date today;
    private String dateTemp;
    
    public static OffsetDate getInstance()
    {
        if(local==null)
        {
            local=new OffsetDate();
        }
        return local;
    }

    public String OffsetYears(int tp)
    {
        try
        {
        today=new Date();
        SimpleDateFormat kiper=new SimpleDateFormat("yyyy-MM-dd");
        String tep=kiper.format(today);
        Calendar startDate=Calendar.getInstance();
        today=new SimpleDateFormat("yyyy-MM-dd").parse(tep);
        startDate.setTime(today);
        startDate.add(Calendar.YEAR, tp); //Offset 1 year
        Date endDate=startDate.getTime();
        SimpleDateFormat clar=new SimpleDateFormat("yyyy-MM-dd");
        dateTemp=clar.format(endDate);
        }
        catch(Exception e)
        {
            dateTemp="N/A";
            e.printStackTrace();
        }
        return dateTemp;
    }

    public String OffsetMonths(int tp)
    {
        try
        {
        today=new Date();
        SimpleDateFormat kiper=new SimpleDateFormat("yyyy-MM-dd");
        String tep=kiper.format(today);
        Calendar startDate=Calendar.getInstance();
        today=new SimpleDateFormat("yyyy-MM-dd").parse(tep);
        startDate.setTime(today);
        startDate.add(Calendar.MONTH, tp); //Offset 1 month
        Date endDate=startDate.getTime();
        SimpleDateFormat clar=new SimpleDateFormat("yyyy-MM-dd");
        dateTemp=clar.format(endDate);
        }
        catch(Exception e)
        {
            dateTemp="N/A";
            e.printStackTrace();
        }
        return dateTemp;
    }

    public String OffsetWeeks(int tp)
    {
        try
        {
        today=new Date();
        SimpleDateFormat kiper=new SimpleDateFormat("yyyy-MM-dd");
        String tep=kiper.format(today);
        Calendar startDate=Calendar.getInstance();
        today=new SimpleDateFormat("yyyy-MM-dd").parse(tep);
        startDate.setTime(today);
        startDate.add(Calendar.WEEK_OF_MONTH, tp); //Offset 1 week
        Date endDate=startDate.getTime();
        SimpleDateFormat clar=new SimpleDateFormat("yyyy-MM-dd");
        dateTemp=clar.format(endDate);
        }
        catch(Exception e)
        {
            dateTemp="N/A";
            e.printStackTrace();
        }
        return dateTemp;
    }


}
